-- 1.参数列表
-- 1.1 优惠券id
local voucherId = ARGV[1]
-- 1.2 用户id
local userId = ARGV[2]
-- 1.3 订单id
local orderId = ARGV[3]
-- 2.数据key
-- 2.1 库存key
local stockKey = "seckill:stock:" .. voucherId
-- 2.2 订单key
local orderKey = "seckill:order:" .. voucherId
-- 3.判断库存是否充足
if (tonumber(redis.call("get", stockKey)) <= 0) then
    -- 3.1.库存不足
    return 1
end
-- 4.判断用户是否重复购买
if (redis.call("sismember", orderKey, userId) == 1) then
    -- 4.1.用户重复购买
    return 2
end
-- 5.扣减库存
redis.call("incrby", stockKey, -1)
-- 6.记录订单
redis.call("sadd", orderKey, userId)
-- 6.1 把订单信息保存到消息队列中
redis.call("xadd", "stream.orders", "*", "userId", userId, "voucherId", voucherId, "id", orderId)
return 0